package com.xinghou.platform.dao.main.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.Sphere;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.mongodb.WriteResult;
import com.xinghou.platform.model.main.UserLocation_Geo;
import com.xinghou.platform.util.StringUtils;

/**
 * 
 * @author xuzhen
 *
 */
@Service
public class UserLocationMONGOImpl {

/*	@Resource
	private MongoTemplate mongoTemplate;
	
	*//**
	 * 创建mongo数据表
	 *//*
	*//*public void createCollection() {
        if (!this.mongoTemplate.collectionExists(UserLocation_Geo.class)) {  
            this.mongoTemplate.createCollection(UserLocation_Geo.class);  
        }  
    }  *//*
	
	*//*@Override
	public void insert(UserLocation_Geo userLocation_Geo) {
		mongoTemplate.insert(userLocation_Geo);
		
	}*//*
	
	*//*public void delete(UserLocation_Geo userLocation_Geo) {
		mongoTemplate.remove(userLocation_Geo);
	}*//*

	*//*@Override
	public UserLocation_Geo findOne(int id) {
		// TODO Auto-generated method stub
		return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public UserLocation_Geo findOnebyUserid(int userid) {
		// TODO Auto-generated method stub
		return mongoTemplate.findOne(new Query(Criteria.where("userId").is(userid)), UserLocation_Geo.class);
	}
*//*
	*//*@Override
	public List<UserLocation_Geo> findAll() {
		// TODO Auto-generated method stub
		return mongoTemplate.find(new Query(), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public List<UserLocation_Geo> nearSphere(Point point,int limit,int skip,double maxDistance) {
		Criteria criteria = Criteria.where("pos").nearSphere(point).maxDistance(maxDistance);

		Query query = new Query(criteria);
		return mongoTemplate.find(query.limit(limit).skip(skip), UserLocation_Geo.class);
	}*//*
	
	*//*public List<UserLocation_Geo> nearSphere(Point point,int limit,int skip,double maxDistance,int type) {
        long  s1=System.currentTimeMillis();
		Query query = new Query();
		query.addCriteria(Criteria.where("pos").nearSphere(point).maxDistance(maxDistance));
		if(type == 1){
			query.addCriteria(Criteria.where("userType").is(0));
		}else{
			query.addCriteria(Criteria.where("userType").is(1));
		}
		
	
		System.out.println(limit+"---"+skip);
		System.out.println("耗时："+(System.currentTimeMillis()-s1)/1000);
		return mongoTemplate.find(query.limit(limit).skip(skip), UserLocation_Geo.class);
	}*//*
	
	*//*public List<UserLocation_Geo> nearSphereforUser(Point point,int limit,int skip,double maxDistance,int storeid) {
		Query query = new Query();
		query.addCriteria(Criteria.where("pos").nearSphere(point).maxDistance(maxDistance));
		query.addCriteria(Criteria.where("storeid").is(storeid));
		return mongoTemplate.find(query.limit(limit).skip(skip), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public List<UserLocation_Geo> near(Point point, String orderFeild,
			int limit, double maxDistance) {
		Criteria criteria = Criteria.where("pos").near(point).maxDistance(maxDistance);

		Query query = new Query(criteria);
		if(orderFeild != null){
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		}
		return mongoTemplate.find(query.limit(limit), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public List<UserLocation_Geo> getPersonosSortById(String cityCode, int limit) {
		Criteria criteria = new Criteria();
		Query query = new Query(criteria);
		if(StringUtils.isNotBlank(cityCode))
			query.addCriteria(Criteria.where("cityCode").in(cityCode));
		query.with(new Sort(new Sort.Order(Direction.DESC, "_id")));
		
		return mongoTemplate.find(query.limit(limit), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public List<UserLocation_Geo> withinSphere(Point center, String orderFeild,
			int limit, double radius) {
		Circle circle = new Circle(center, radius);
		Criteria criteria = Criteria.where("pos").withinSphere(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), UserLocation_Geo.class);
	}*//*

	*//*@Override
	public List<UserLocation_Geo> within(Point center, String orderFeild,
			int limit, double radius) {
		Sphere circle = new Sphere(center, radius);
		Criteria criteria = Criteria.where("pos").within(circle);
		Query query = new Query(criteria);
		if(orderFeild != null)
			query.with(new Sort(new Sort.Order(Direction.DESC, orderFeild)));
		return mongoTemplate.find(query.limit(limit), UserLocation_Geo.class);
	}
*//*
	*//*@Override
	public List<UserLocation_Geo> loginSortByCityCode(String cityCode, int limit) {
		Criteria criteria = new Criteria();
		Query query = new Query(criteria);
		if(StringUtils.isNotBlank(cityCode))
			query.addCriteria(Criteria.where("cityCode").in(cityCode));
		query.with(new Sort(new Sort.Order(Direction.DESC, "_id")));
		
		return mongoTemplate.find(query.limit(limit), UserLocation_Geo.class);
	}*//*

	*//*public boolean update(UserLocation_Geo userLocation_Geo) {
		boolean result = false;
		
		Query query = new Query();  
        query.addCriteria(new Criteria("userId").is(userLocation_Geo.getUserId()));  
        Update update = new Update();  
        update.set("deviceId", userLocation_Geo.getDeviceId());  
        update.set("pos", userLocation_Geo.getPos());
        update.set("cityCode", userLocation_Geo.getCityCode());
        update.set("lastLoginTime", userLocation_Geo.getLastLoginTime());
        update.set("storeid", userLocation_Geo.getStoreid());
        update.set("userType", userLocation_Geo.getUserType());
        WriteResult writeResult = this.mongoTemplate.updateFirst(query, update, UserLocation_Geo.class);  
		
        if (!writeResult.wasAcknowledged()) {
            result = true;
        }  
		
		return result;
		
	}*//*
	
	*//*public boolean updateStoreid(UserLocation_Geo userLocation_Geo) {
		boolean result = false;
		
		Query query = new Query();  
        query.addCriteria(new Criteria("userId").is(userLocation_Geo.getUserId()));  
        Update update = new Update();
        update.set("storeid", userLocation_Geo.getStoreid());
        update.set("userType", userLocation_Geo.getUserType());
        WriteResult writeResult = this.mongoTemplate.updateFirst(query, update, UserLocation_Geo.class);  
		
        if (!writeResult.wasAcknowledged()) {
            result = true;
        }  
		
		return result;
		
	}*//*
	
	
	*//*public boolean updateStoreidAll(UserLocation_Geo userLocation_Geo) {
		boolean result = false;
		
		Query query = new Query();  
        query.addCriteria(new Criteria("storeid").not());  
        Update update = new Update();
        update.set("storeid", userLocation_Geo.getStoreid());
        WriteResult writeResult = this.mongoTemplate.updateFirst(query, update, UserLocation_Geo.class);  
		
        if (!writeResult.wasAcknowledged()) {
            result = true;
        }  
		
		return result;
		
	}
*/
}
